package bbs.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import bbs.annotation.GlobalInterceptor;
import bbs.annotation.VerifyParam;
import bbs.controller.base.ABaseController;
import bbs.entity.dto.SessionWebUserDto;
import bbs.entity.dto.UserMessageCountDto;
import bbs.entity.enums.ArticleStatusEnum;
import bbs.entity.enums.MessageTypeEnum;
import bbs.entity.enums.ResponseCodeEnum;
import bbs.entity.enums.UserStatusEnum;
import bbs.entity.po.UserInfo;
import bbs.entity.query.ForumArticleQuery;
import bbs.entity.query.LikeRecordQuery;
import bbs.entity.query.UserIntegralRecordQuery;
import bbs.entity.query.UserMessageQuery;
import bbs.entity.vo.PaginationResultVO;
import bbs.entity.vo.ResponseVO;
import bbs.entity.vo.web.ForumArticleVO;
import bbs.entity.vo.web.UserInfoVO;
import bbs.exception.BusinessException;
import bbs.service.*;
import bbs.utils.CopyTools;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController("userCenterController")
@RequestMapping({"/ucenter"})
public class UserCenterController extends ABaseController {
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private ForumArticleService forumArticleService;

    @Resource
    private UserMessageService userMessageService;

    @Resource
    private LikeRecordService likeRecordService;

    @Resource
    private UserIntegralRecordService userIntegralRecordService;

    @RequestMapping({"/getUserInfo"})
    @GlobalInterceptor(checkParams = true)
    public ResponseVO getUserInfo(@VerifyParam(required = true) String userId) {
        UserInfo userInfo = this.userInfoService.getUserInfoByUserId(userId);
        if (null == userInfo || UserStatusEnum.DISABLE.getStatus().equals(userInfo.getStatus()))
            throw new BusinessException(ResponseCodeEnum.CODE_404);

        ForumArticleQuery forumArticleQuery = new ForumArticleQuery();
        forumArticleQuery.setUserId(userId);
        forumArticleQuery.setStatus(ArticleStatusEnum.AUDIT.getStatus());
        Integer postCount = this.forumArticleService.findCountByParam(forumArticleQuery);
        UserInfoVO userInfoVO = (UserInfoVO) CopyTools.copy(userInfo, UserInfoVO.class);
        userInfoVO.setPostCount(postCount);

        LikeRecordQuery likeRecordQuery = new LikeRecordQuery();
        likeRecordQuery.setAuthorUserId(userId);
        Integer likeCount = this.likeRecordService.findCountByParam(likeRecordQuery);
        userInfoVO.setLikeCount(likeCount);
        userInfoVO.setCurrentIntegral(userInfo.getCurrentIntegral());
        return getSuccessResponseVO(userInfoVO);
    }

    @RequestMapping({"/updateUserInfo"})
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO updateUserInfo(HttpSession session, Integer sex, @VerifyParam(max = 100) String personDescription, MultipartFile avatar) {
        SessionWebUserDto userDto = getUserInfoFromSession(session);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userDto.getUserId());
        userInfo.setSex(sex);
        userInfo.setPersonDescription(personDescription);
        this.userInfoService.updateUSerInfo(userInfo, avatar);
        return getSuccessResponseVO(null);
    }

    @RequestMapping({"/loadUserIntegralRecord"})
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO loadUserIntegralRecord(HttpSession session, Integer pageNo, String createTimeStart, String createTimeEnd) {
        UserIntegralRecordQuery userIntegralRecordQuery = new UserIntegralRecordQuery();
        userIntegralRecordQuery.setUserId(getUserInfoFromSession(session).getUserId());
        userIntegralRecordQuery.setPageNo(pageNo);
        userIntegralRecordQuery.setCreateTimeStart(createTimeStart);
        userIntegralRecordQuery.setCreateTimeEnd(createTimeEnd);
        userIntegralRecordQuery.setOrderBy("record_id desc");
        PaginationResultVO paginationResultVO = this.userIntegralRecordService.findListByPage(userIntegralRecordQuery);
        return getSuccessResponseVO(paginationResultVO);
    }

    @RequestMapping({"/loadUserArticle"})
    @GlobalInterceptor(checkParams = true)
    public ResponseVO loadUserArticle(HttpSession session, @VerifyParam(required = true) String userId, @VerifyParam(required = true) Integer type, Integer pageNo) {
        UserInfo userInfo = this.userInfoService.getUserInfoByUserId(userId);
        if (null == userInfo || UserStatusEnum.DISABLE.getStatus().equals(userInfo.getStatus()))
            throw new BusinessException(ResponseCodeEnum.CODE_404);

        ForumArticleQuery forumArticleQuery = new ForumArticleQuery();
        forumArticleQuery.setOrderBy("post_time desc");
        forumArticleQuery.setPageNo(pageNo);

        if (type == 0) {
            forumArticleQuery.setUserId(userId);
        } else if (type == 1) {
            forumArticleQuery.setCommentUserId(userId);
        } else if (type == 2) {
            forumArticleQuery.setLikeUserId(userId);
        }
        SessionWebUserDto userDto = getUserInfoFromSession(session);
        if (userDto != null) {
            forumArticleQuery.setCurrentUserId(userDto.getUserId());
        } else {
            forumArticleQuery.setStatus(ArticleStatusEnum.AUDIT.getStatus());
        }
        PaginationResultVO paginationResultVO = this.forumArticleService.findListByPage(forumArticleQuery);

        return getSuccessResponseVO(convert2PaginationVO(paginationResultVO, ForumArticleVO.class));
    }

    @RequestMapping({"/getMessageCount"})
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getMessageCount(HttpSession session) {
        SessionWebUserDto userDto = getUserInfoFromSession(session);
        if (null == userDto)
            return getSuccessResponseVO(new UserMessageCountDto());
        return getSuccessResponseVO(this.userMessageService.getUserMessageCount(userDto.getUserId()));
    }

    @RequestMapping({"/loadMessageList"})
    @GlobalInterceptor(checkLogin = true, checkParams = true)
    public ResponseVO loadMessageList(HttpSession session, @VerifyParam(required = true) String code, Integer pageNo) {
        MessageTypeEnum typeEnum = MessageTypeEnum.getByCode(code);
        if (null == typeEnum)
            throw new BusinessException(ResponseCodeEnum.CODE_600);

        SessionWebUserDto userDto = getUserInfoFromSession(session);
        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setPageNo(pageNo);
        userMessageQuery.setReceivedUserId(userDto.getUserId());
        userMessageQuery.setMessageType(typeEnum.getType());
        userMessageQuery.setOrderBy("message_id desc");
        PaginationResultVO resultVO = this.userMessageService.findListByPage(userMessageQuery);

        if (pageNo == null || pageNo.intValue() == 1)
            this.userMessageService.readMessageByType(userDto.getUserId(), typeEnum.getType());

        return getSuccessResponseVO(resultVO);
    }
}
